package com.dllo.lanou.cookapp.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;

import com.dllo.lanou.cookapp.R;
import com.dllo.lanou.cookapp.entity.HeadViewPageEntity;
import com.dllo.lanou.cookapp.entity.HeadViewPageResultEntity;
import com.dllo.lanou.cookapp.entity.MenuDescription;
import com.dllo.lanou.cookapp.utils.NetworkImageHolderView;
import com.dllo.lanou.cookapp.volly.GetVolleyImage;
import com.dllo.lanou.cookapp.volly.VolleySingleton;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class DetatViewPagerActivity extends AppCompatActivity {
    private long id;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.food)
    TextView food;
    @Bind(R.id.des)
    TextView des;
    @Bind(R.id.foodimg)
    ImageView foodimg;
    @Bind(R.id.do_webview)
    WebView doWebView;
    WebSettings settings;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detatl);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        id = intent.getLongExtra("id", 0);
        Log.i("id", id + "");
        requestData();

    }

    //请求数据
    public void requestData() {
        String path = "http://apis.baidu.com/tngou/cook/show";
        String httpArg = "id=" + id;
        String url = path + "?" + httpArg;
        Log.i("iddd", url + "");
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();

                MenuDescription entity = gson.fromJson(response, MenuDescription.class);
                if (entity != null) {
                    //resultEntities = entity.getTngou();
                    // HeadViewPageResultEntity resultEntity = resultEntities.get(0);
                    System.out.println("第一条信息:" + entity.getDescription());
                    System.out.println("第一条信息:" + entity.getFood());
                    System.out.println("第一条信息:" + entity.getMessage());
                    System.out.println("第一条信息:" + entity.getName());
                    System.out.println("第一条信息:" + entity.getKeywords());
                    System.out.println("第一条信息:" + entity.getImg());
                    name.setText(entity.getName());
                    food.setText("材料："+entity.getFood());
                    des.setText("描述：" + entity.getDescription());
                    String imgurl = "http://tnfs.tngou.net/img" + entity.getImg();
                    GetVolleyImage.getBitmapRequest(imgurl,foodimg);

                    String htmlString = entity.getMessage();
                    final String mimrTypeString = "text/html";
                    final String encoding = "utf-8";
                    settings = doWebView.getSettings();
                    settings.setSupportZoom(true);
                    doWebView.loadDataWithBaseURL("fake://not/needed", htmlString, mimrTypeString, encoding, "");
                    settings.setTextZoom(150);
                   settings.setTextSize(WebSettings.TextSize.LARGEST);

                    doWebView.setInitialScale(100);


                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            //在StringRespset结束之前写{}在大括号里面写 getHeaders() 自动重写方法
            //用Map方法键值对形势添加
            //添加请求头
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("apikey", "4ee0087cd4103e3acbab0f8efbf0bbd1");
                return map;
            }


        };
        VolleySingleton.getInstance().addRequest(request);
    }

}
